/**
 * Project Name:jiuselu-parent
 * File Name:PayNotification.java
 * Package Name:com.fxp.project.jiuselu.web.business.v1.controller.pay.yeepay
 * Date:2021年12月03日 16:36
 * Copyright (c) 2021, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.jiuselu.web.business.v1.controller.pay.yeepay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fxp.project.jiuselu.commons.date.DateUtil;
import com.fxp.project.jiuselu.commons.sm4.Sm4GcmUtils;
import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.business.v1.request.order.ReqNotify;
import com.fxp.project.jiuselu.web.config.YopOprConstants;
import com.fxp.project.jiuselu.web.domain.CommonResult;
import com.fxp.project.jiuselu.web.models.order.service.IJslOrderArrearsService;
import com.fxp.project.jiuselu.web.models.order.service.IJslOrderService;
import com.fxp.project.jiuselu.web.models.order.vo.JslOrderVo;
import com.fxp.project.jiuselu.web.models.user.service.IJslUserService;
import lombok.extern.flogger.Flogger;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ClassName:PayNotification<br/>
 * Function: 易付宝的支付回调<br/>
 * Date:2021年12月03日 16:36<br/>
 *
 * @author Jason.Wang
 * @version 1.0.0
 * @see
 * @since JDK 8
 */
@Controller
@RequestMapping("/v1/pay/yeepay")
public class PayNotification extends BaseController {

    private Lock lock=new ReentrantLock();

    @Autowired
    private YopOprConstants yopOprConstants;

    @Autowired
    private IJslOrderService jslOrderService;
    /**
     * 时间格式插件
     */
    private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * notification: 接收异步通知<br/>
     *
     * @param notify
     * @param request
     * @param response
     * @return
     * @author Jason.Wang
     * @date 2021/12/26 18:56
     */
    @RequestMapping("notification")
    @CrossOrigin
    public void notification(ReqNotify notify, HttpServletRequest request, HttpServletResponse response) {
        lock.lock();
        if (yopOprConstants.getAppKey().equals(notify.getCustomerIdentification())) {
            String resultStr = null;
            try {
                resultStr = Sm4GcmUtils.decrypt_GCM_NoPadding(yopOprConstants.getAesSecretKey(), notify.getCipherText(), notify.getNonce(), notify.getAssociatedData());
                System.out.println(resultStr);
                /**
                 * <pre>
                 * {
                 *     "bankTrxId":"4200001392202112296151049582",
                 *     "preAuthAccountTime":"",
                 *     "orderId":"QK20211123153607803803201",
                 *     "bankOrderId":"5142530079211229",
                 *     "platformType":"WECHAT",
                 *     "uniqueOrderNo":"1001202112290000003043968657",
                 *     "ypSettleAmount":"0.01",
                 *     "orderAmount":"0.01",
                 *     "payAmount":"0.01",
                 *     "requestDate":"2021-12-29 00:11:34",
                 *     "openID":"oPYNS6ipCshg7WcOIaZR70s5IcC0",
                 *     "paySuccessDate":"2021-12-29 00:12:16",
                 *     "customerFee":"0.00",
                 *     "cardType":"CREDIT",
                 *     "bankPaySuccessDate":"2021-12-29 00:12:16",
                 *     "fundProcessType":"REAL_TIME",
                 *     "bankId":"CMBCHINA",
                 *     "parentMerchantNo":"10034133336",
                 *     "yeepayPromotionDTOList":"[]",
                 *     "paymentProduct":"SCCANPAY",
                 *     "merchantNo":"10034274377",
                 *     "status":"SUCCESS"
                 * }
                 * </pre>
                 */
                JSONObject jsonObject = JSON.parseObject(resultStr);
                System.out.println("wj___________________"+resultStr);
                if (!jsonObject.isEmpty() && jsonObject.containsKey("status") && "SUCCESS".equalsIgnoreCase(jsonObject.getString("status"))) {
                    String orderId = jsonObject.getString("orderId");
                    String paySuccessDate = jsonObject.getString("paySuccessDate");
                    String bankTrxId = jsonObject.getString("uniqueOrderNo");
                    if(jslOrderService.updateOrderPayMentInfo(orderId,LocalDateTime.parse(paySuccessDate, dateTimeFormatter),bankTrxId)){
                        render(response, "text/plain;charset=UTF-8", "SUCCESS");
                    }else{
                        render(response, "text/plain;charset=UTF-8", "FAILD");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                render(response, "text/plain;charset=UTF-8", "FAILD");
            }
        } else {
            render(response, "text/plain;charset=UTF-8", "FAILD");
        }
        lock.unlock();
    }
}
